package service

import (
	"errors"
	"fmt"
	"go_my_framework/app/dao"
	"go_my_framework/app/global/request"
	"go_my_framework/app/model"
	"go_my_framework/util/sli"
	"html/template"
	"strings"
)

type CategoryService struct {
}

type CategoryTree struct {
	Id     int
	Pid    int
	Level  int
	SubLen int
	Name   string
	Repeat string
	Sub    []CategoryTree
}

type CategoryFormatTree struct {
	Id      int    `json:"id"`
	PId     int    `json:"pId"`
	Name    string `json:"name"`
	Checked bool   `json:"checked"`
}

// 获取管理员列表
func (c *CategoryService) GetList(f request.CategoryForm) ([]model.Category, int64, error) {
	if f.Page == 0 {
		f.Page = 1
	}
	if f.Limit == 0 {
		f.Limit = 10
	}
	data, count, err := dao.NewCategoryDaoImpl().GetList(f.Page, f.Limit, f.Name)
	if err != nil {
		return []model.Category{}, 0, errors.New(err.Error())
	}

	return data, count, nil
}

func (c *CategoryService) GetRow(id int) (model.Category, error) {
	purview := dao.NewCategoryDaoImpl().FindRow(id)
	return purview, nil
}

func (c *CategoryService) GetRowByName(name string) model.Category {
	purview, err := dao.NewCategoryDaoImpl().FindRowByName(name)
	if err != nil {
		return model.Category{}
	}
	return purview
}

func (c *CategoryService) Add(name string, pid int, sort int) error {
	exist := c.GetRowByName(name)
	if exist.Id != 0 {
		return errors.New("已存在同名分类")
	}
	return dao.NewCategoryDaoImpl().Create(name, pid, sort)
}

func (c *CategoryService) Edit(id int, name string, pid int, sort int) error {
	return dao.NewCategoryDaoImpl().Update(id, name, pid, sort)
}

func (c *CategoryService) GetTree(Pid int) string {
	list, _, _ := dao.NewCategoryDaoImpl().GetList(1, 10000, "")
	listTree := c.Tree(list, 0, 0)
	treeHtml := c.TreeHtml(listTree, Pid)

	return treeHtml
}

func (c *CategoryService) GetAll(selectSli []int) []CategoryFormatTree {
	data, _, _ := dao.NewCategoryDaoImpl().GetList(1, 10000, "")
	list := c.CategoryFormatTreeData(data, selectSli)
	return list
}

func (c *CategoryService) DelRow(id int) error {
	return dao.NewCategoryDaoImpl().Del(id)
}

//func (p *CategoryService) GetMenusTree(roleId int, allowIds []int) string {
//	list, _, _ := dao.NewCategoryDaoImpl().GetList(1, 10000, "")
//	listTree := Tree(list, 0, 0)
//
//	// 角色拥有的权限
//	allowList := p.GetPurviewByIds(allowIds)
//	urls := make([]int, len(allowList), len(allowList))
//	for k, v := range allowList {
//		urls[k] = v.Id
//	}
//	return MenusTreeHtml(listTree, roleId, urls)
//}

//func (p *CategoryService) GetPurviewByIds(ids []int) []model.Category {
//	purviewList, _ := dao.NewCategoryDaoImpl().GetListByIds(ids, []string{"*"})
//	return purviewList
//}


func (c *CategoryService) Tree(list []model.Category, pid int, level int) []CategoryTree {
	var returnList []CategoryTree
	for _, v := range list {
		if v.Pid == pid {
			var tmp CategoryTree
			tmp.Id = v.Id
			tmp.Name = v.Name
			tmp.Pid = v.Pid
			tmp.Level = level
			tmp.Repeat = strings.Repeat(string(template.HTML("&nbsp;&nbsp;")), level) + "├─"
			tmp.Sub = c.Tree(list, tmp.Id, level+1)
			tmp.SubLen = len(tmp.Sub)
			returnList = append(returnList, tmp)
		}
	}
	return returnList
}

func (c *CategoryService) TreeHtml(listTree []CategoryTree, Pid int) string {
	treeHtml := ""
	for _, v := range listTree {
		selectStr := ""
		if Pid != 0 && Pid == v.Id {
			selectStr = "selected"
		} else {
			selectStr = ""
		}
		treeHtml += fmt.Sprintf("<option value='%d' %s>%s %s </option>", v.Id, selectStr, v.Repeat, v.Name)
		if v.SubLen > 0 {
			treeHtml += c.TreeHtml(v.Sub, Pid)
		}
	}
	return treeHtml
}

func (c *CategoryService) CategoryFormatTreeData(list []model.Category, selectSli []int) []CategoryFormatTree {
	if len(list) == 0 {
		return []CategoryFormatTree{}
	}
	var returnList []CategoryFormatTree
	for _, v := range list {
		var tmp CategoryFormatTree
		tmp.Id = v.Id
		tmp.PId = v.Pid
		tmp.Name = v.Name
		if len(selectSli) > 0 && sli.InArray(tmp.Id, selectSli) {
			tmp.Checked = true
		} else {
			tmp.Checked = false
		}
		returnList = append(returnList, tmp)
	}
	return returnList
}

//func MenusTreeHtml(listTree []PurviewTree, roleId int, allowIds []int) string {
//	treeHtml := ""
//	for _, v := range listTree {
//		// 不属于该玩家的权限不展示
//		if roleId != e.SuperRoleId && !sli.InArray(v.Id, allowIds) {
//			continue
//		}
//		treeHtml += "" +
//			"<li>" +
//			"<a href=\"javascript:;\">" +
//			"<i class=\"iconfont left-nav-li\" lay-tips=\"{$arrVal.name}\">&#xe723;</i>" +
//			"<cite>" + v.Name + "</cite>" +
//			"<i class=\"iconfont nav_right\">&#xe697;</i>" +
//			"</a>" +
//			"<ul class=\"sub-menu\">"
//		if v.SubLen > 0 {
//			for _, vv := range v.Sub {
//				// 不属于该玩家的权限不展示
//				if roleId != e.SuperRoleId && !sli.InArray(vv.Id, allowIds) {
//					continue
//				}
//				treeHtml += "" +
//					"<li>" +
//						"<a onclick=\"xadmin.add_tab('" + vv.Name + "','" + vv.Url + "')\">" +
//							"<i class=\"iconfont\">&#xe6a7;</i>" +
//							"<cite>" + vv.Name + "</cite>" +
//						"</a>" +
//					"</li>"
//			}
//		}
//		treeHtml += "</ul></li>"
//	}
//	return treeHtml
//}

func (c *CategoryService) GetCateMap() map[int]string {
	cateSvc := CategoryService{}
	cateIdAndName := make(map[int]string)
	category := cateSvc.GetAll(nil)
	for _, v := range category {
		cateIdAndName[v.Id] = v.Name
	}
	return cateIdAndName
}